{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "is_executing": false
    }
   },
   "source": [
    "3.10 多层感知机的简洁实现\n",
    "下面我们使用tensorflow来实现上一节中的多层感知机。首先导入所需的包或模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "import sys\n",
    "sys.path.append(\"..\") \n",
    "from tensorflow import keras\n",
    "fashion_mnist = keras.datasets.fashion_mnist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.10.1 定义模型\n",
    "和softmax回归唯一的不同在于，我们多加了一个全连接层作为隐藏层。它的隐藏单元个数为256，并使用ReLU函数作为激活函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model = tf.keras.models.Sequential([\n",
    "                                    tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
    "                                    tf.keras.layers.Dense(256, activation='relu',),\n",
    "                                    tf.keras.layers.Dense(10, activation='softmax')\n",
    "                                    ])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.10.2 读取数据并训练模型\n",
    "我们使用与3.7节中训练softmax回归几乎相同的步骤来读取数据并训练模型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/5\n",
      "60000/60000 [==============================] - 2s 36us/sample - loss: 0.9844 - accuracy: 0.7100 - val_loss: 0.5575 - val_accuracy: 0.7945\n",
      "Epoch 2/5\n",
      "60000/60000 [==============================] - 2s 33us/sample - loss: 0.5038 - accuracy: 0.8141 - val_loss: 0.6090 - val_accuracy: 0.7702\n",
      "Epoch 3/5\n",
      "60000/60000 [==============================] - 2s 35us/sample - loss: 0.4334 - accuracy: 0.8396 - val_loss: 0.4691 - val_accuracy: 0.8341\n",
      "Epoch 4/5\n",
      "60000/60000 [==============================] - 2s 34us/sample - loss: 0.3969 - accuracy: 0.8535 - val_loss: 0.4293 - val_accuracy: 0.8494\n",
      "Epoch 5/5\n",
      "60000/60000 [==============================] - 2s 31us/sample - loss: 0.3754 - accuracy: 0.8621 - val_loss: 0.4657 - val_accuracy: 0.8288\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x5ad1df28d0>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fashion_mnist = keras.datasets.fashion_mnist\n",
    "(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()\n",
    "x_train = x_train / 255.0\n",
    "x_test = x_test / 255.0\n",
    "model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.5),\n",
    "             loss = 'sparse_categorical_crossentropy',\n",
    "             metrics=['accuracy'])\n",
    "model.fit(x_train, y_train, epochs=5,\n",
    "              batch_size=256,\n",
    "              validation_data=(x_test, y_test),\n",
    "              validation_freq=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "小结\n",
    "通过Tensorflow2.0可以更简洁地实现多层感知机。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
